package com.offcn.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import lombok.Data;
import lombok.ToString;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@ToString
@Data
public class OssTemplate {

    private String endpoint;

    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;

    public String upload(InputStream inputStream, String fileName){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String folderName = simpleDateFormat.format(new Date());
        fileName = UUID.randomUUID().toString().replace("-","")+""+fileName;
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject(bucketName,"pic/"+folderName+"/"+fileName,inputStream);
        ossClient.shutdown();
        String url = "http://"+bucketName+"."+endpoint+"/pic/"+folderName+"/"+fileName;
        System.out.println("上传成功");
        return url;
    }
}